package com.qczl.pme.controller.stb;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qczl.core.controller.BaseController;
import com.qczl.model.Json;
import com.qczl.pme.service.AreaService;
import com.qczl.pme.vo.AreaVo;

/**
 * 区域控制器
 * 
 * @author SongR
 *
 */
@Controller
@RequestMapping(value = "/stb/area")
public class StbAreaController extends BaseController {
	Logger log = Logger.getLogger(this.getClass());

	@Autowired
	AreaService areaservice;

	/**
	 * 根据机顶盒获取镇级区域
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/town/v1", method = { RequestMethod.GET, RequestMethod.POST })
	public Json getTownByStbV1(String stbno,int start,int size) {
		Json j = new Json();
		try {
			List<AreaVo> vos = areaservice.getTownByStbV1(stbno,start,size);
			if(vos.size()>0){
				j.setSize(areaservice.countTownByStbV1(stbno));
				j.setMsg("成功获取机顶盒镇级！");
				j.setResult(vos);
				j.setLength(new Long(vos.size()));
			}else{
				j.setSize(new Long(0));
				j.setState(404);
				j.setMsg("暂无镇级！");
			}
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("获取机顶盒镇级失败！");
			log.error("获取机顶盒镇接口异常！",e);
		}
		return j;
	}
	
	/**
	 * 获取机顶盒区域
	 * @作者:SongR
	 * @版本:v1
	 * @日期:2017年9月21日 下午5:40:44
	 */
	@ResponseBody
	@RequestMapping(value="/get/v1",method={RequestMethod.GET,RequestMethod.POST})
	public Json getByStbV1(String stbno){
		Json j = new Json();
		try {
			AreaVo vo =areaservice.getByStbV1(stbno);
			if(vo!=null){
				j.setMsg("成功获取机顶盒区域！");
				j.setResult(vo);
			}else{
				j.setMsg("该机顶盒暂无权限！");
			}
		} catch (Exception e) {
			j.setMsg("获取机顶盒区域失败！");
			j.setSuccess(false);
			log.error("获取机顶盒区域接口异常！",e);
		}
		return j;
	}
	/**
	 * 获取机顶盒镇列表
	 * @作者:SongR
	 * @版本:v1
	 * @日期:2017年9月21日 下午5:36:26
	 */
	@ResponseBody
	@RequestMapping(value="/child/v1",method={RequestMethod.GET,RequestMethod.POST})
	public Json getTownByStbV1(String stbno,String pid,String start,String size){
		Json j = new Json();
		try {
			List<AreaVo> vos =areaservice.getChildByStbV1(stbno,pid,start,size);
			if(vos.size()>0){
				j.setMsg("成功获取子级区域！");
				j.setResult(vos);
				j.setSize(areaservice.countChildByStbV1(stbno,pid));
			}else{
				j.setMsg("该机顶盒暂无权限！");
			}
		} catch (Exception e) {
			j.setMsg("获取子级区域失败！");
			j.setSuccess(false);
			log.error("获取子级区域接口异常！",e);
		}
		return j;
	}
	
	
	
}
